package com.aplink.dellcommercial.activities;

import android.content.IntentFilter;
import android.os.Bundle;

import com.aplink.dellcommercial.R;
import com.aplink.dellcommercial.fragments.MainFragment;
import com.aplink.dellcommercial.network.NetworkConnectivityReceiver;
import com.aplink.dellcommercial.util.DellCommercialUtility;
import com.aplink.generic.GenericActivity;
import com.aplink.generic.GenericFragment;

public class MainActivity extends GenericActivity {
    private NetworkConnectivityReceiver mNetworkConnectivityReceiver;

    @Override
    protected GenericFragment getFragment() {
        check();
        return new MainFragment();
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        mNetworkConnectivityReceiver = new NetworkConnectivityReceiver();
    }

    private void check() {
        int checkState = DellCommercialUtility.checkTimeValidation();
        switch ( checkState ) {
            case 0:
                String messageServerTimeOut = getString( R.string.messageServerTimeOut );
                String titleServerTimeOut = getString( R.string.titleServerTimeOut );
                DellCommercialUtility.showLogoutDialog( MainActivity.this,
                        android.R.drawable.ic_dialog_alert, messageServerTimeOut,
                        titleServerTimeOut );
                break;
            case 1:
                String messageLocalTimeOut = getString( R.string.messageLocalTimeOut );
                String titleLocalTimeOut = getString( R.string.titleLocalTimeOut );
                DellCommercialUtility.showLogoutDialog( MainActivity.this,
                        android.R.drawable.ic_dialog_alert, messageLocalTimeOut,
                        titleLocalTimeOut );
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        check();
        IntentFilter intentFilter = new IntentFilter(
                android.net.ConnectivityManager.CONNECTIVITY_ACTION );
        registerReceiver( mNetworkConnectivityReceiver, intentFilter );
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver( mNetworkConnectivityReceiver );
    }
}
